package com.junzhiit.nengji.cms.web.controller;

import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.bainaeco.framework.util.AssertUtil;
import com.junzhiit.nengji.cms.service.OrdersGoodsService;
import com.junzhiit.nengji.cms.service.OrdersService;
import com.junzhiit.nengji.cms.web.vo.DataTablesParamVO;
import com.junzhiit.nengji.cms.web.vo.OrdersRefundVO;
import com.junzhiit.nengji.cms.web.vo.ResultVO;

import io.swagger.annotations.ApiOperation;

@RestController
@RequestMapping("/orders/goods")
public class OrdersGoodsController extends BaseController {

	@Autowired
	private OrdersService ordersService;

	@Autowired
	private OrdersGoodsService ordersGoodsService;

	@ApiOperation("查询订单详情")
	@RequestMapping(value = "/view", method = RequestMethod.GET)
	public ResultVO goMainView(String orderId, String sessionId) {
		ResultVO result = new ResultVO();
		result.setData(ordersService.findOrderId(orderId));
		return result;
	}

	@ApiOperation("查询订单商品详情")
	@RequestMapping(method = RequestMethod.GET)
	public Map<String, Object> findAll(DataTablesParamVO param, String orderId, String sessionId) {
		return ordersGoodsService.findAll(param, orderId);
	}

	@ApiOperation("查询商品有关订单数")
	@RequestMapping(value = "/count/{goodsId}", method = RequestMethod.GET)
	public ResultVO getOrderCountByGoodsId(@PathVariable String goodsId) {
		AssertUtil.notBlank(goodsId, "goodsId is required");
		return ordersGoodsService.getOrderCountByGoodsId(goodsId);
	}

	@ApiOperation("商家退款订单商品")
	@RequestMapping(value = "/status", method = RequestMethod.PUT)
	public ResultVO updateStatus(OrdersRefundVO ordersRefundVO) {
		ResultVO result = new ResultVO();
		result.setData(ordersGoodsService.update(ordersRefundVO));
		return result;
	}
	
	@ApiOperation("查询订单商品信息")
	@RequestMapping(value = "/price", method = RequestMethod.GET)
	public ResultVO findInfo(String ordersGoodsId) {
		return ordersGoodsService.findInfo(ordersGoodsId);
	}
	
	@ApiOperation("平台申请退款")
	@RequestMapping(value = "/apply", method = RequestMethod.PUT)
	public ResultVO Applyrefund(OrdersRefundVO ordersRefundVO) {
		return ordersGoodsService.applyRefund(ordersRefundVO);
	}
}
